/*
 * @Author: DaTree
 * @Date: 2021-08-02 12:51:25
 * @LastEditTime: 2021-08-04 08:54:40
 * @LastEditors: DaTree
 * @Description: DaTreeEdit
 * @FilePath: \puppeteer_front_services\src\index.js
 * 天将降大任于是人也，必先苦其心志，劳其筋骨，饿其体肤，空乏其身，行拂乱其所为。 ——《孟子•告子下》
 * Copyright 2021 DaTree
 */
console.log('获取命令行参数。')
console.log(process.argv)
const current = new Date();
console.log(current.toLocaleDateString())
console.log(current.toLocaleTimeString())
// 获取自定义参数
process.argv.forEach(x => {
  if (x.split('=').length === 2) {
    const arg = { key: 'FRONT_' + x.split('=')[0].replace('--', '').toUpperCase(), value: x.split('=')[1] }
    console.log(arg);
    process.env[arg.key] = arg.value
  }
})

// 获取自定义参数
// const argv = JSON.parse(process.env.npm_config_argv)
// let idx = 2
// const cooked = argv.cooked
// const length = argv.cooked.length
// while ((idx += 2) <= length) {
//   process.env['FRONT_'+cooked[idx - 2].replace('--', '').toUpperCase()] = cooked[idx - 1]
// }

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox'
    ]
  });
  // const browser = await puppeteer.launch();
  const page = await browser.newPage();
  page.setViewport({
    width: 1920,
    height: 1080
  });
  page.on('load', async function () {
    console.log('onload')
    debugger
    await page.focus('input#kw.s_ipt')
    await page.type('puppeteer自动化测试', { delay: 200 })
    const searchForm = await page.$('#form')
    // await searchBtn
    await searchForm.evaluate(x => x.submit())
  })
  page.on('console', (e) => {
    console.log(e.text())
  })
  console.log('输出地址')
  console.log(process.env.FRONT_ADDRESS)
  console.log(process.env)
  await page.goto(process.env.FRONT_ADDRESS);
  // console.log(page.content)
  // await page.screenshot({ path: 'example.png' });
  // await browser.close();
})();